<?php
namespace app\api\controller;

use think\Request;

class Riddle extends Base
{   

    ##首页广告
    public function advertisement()
    {
		//管理配置
		$info = db('configure')->where(['admin_id'=>$this->accessData['aid']])->field('home_page')->find();
		
		if ($info['home_page'] == 0){
		    err('暂无数据');
		}
		
        $res = db('extension')->field('id,img,url')->where(['type'=>1,'sort'=>1,'admin_id'=>$this->accessData['aid']])->find();
        
        $res['img'] = $this->game_img($res['img']);
        if (!$res){
            err('暂无数据');
        }else{
            suc('',$res);
        }
        
    }
    
    ##开始答题
    public function Customs()
    {
        //查询上一次关卡
        $where['admin_id'] = $this->accessData['aid'];
        $where['user_id'] = $this->accessData['uid'];
        $res = db('subject_statistics')->where($where)->field('checkpoint')->find();

        $where = [];
        if ($res){
            $where['id'] = $res['checkpoint'];
        }else{
            $where['id'] = 1;
        }

        $info = db('riddle_title')->where($where)->field('id,question,numbers,false_answer,prompt')->find();

        $checkpoint = $info['id'];

        //判断关卡弹窗
        $str = db('configure')->where(['admin_id'=>$this->accessData['aid']])->field('checkpoint')->find();
        
        $customs = explode(",", $str['checkpoint']);
        
        $info['state'] = 0;
        foreach ($customs as $k => $v){
           if ($checkpoint == $v){
               $info['state'] = 1;
           }
        }
 
        if (!$info){
            err('暂无数据');
        }else{
            //增加挑战记录
            if ($res){
                db('subject_statistics')
                ->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])
                ->update(['checkpoint'=>$info['id']]);
            }else{
                $data['admin_id'] = $this->accessData['aid'];
                $data['user_id'] = $this->accessData['uid'];
                $data['checkpoint'] = $info['id'];
                $data['create_time'] = date("Y-m-d H:i:s");
                $data['game_number'] = 1;
                db('subject_statistics')->insert($data);
            }
            suc('',$info);
        }
    }
    
    ##提交答案
    public function answer()
    {
        $post = request()->only(['id','answer'],'post');
        
        if (empty($post['id'])){
            err('题目ID不能为空');
        }
        
        if (empty($post['answer'])){
            err('答案不能为空');
        }

        $res = db('riddle_title')->where(['id'=>$post['id'],'answer'=>$post['answer']])->find();

        
        if (!$res){
            err('答错了');
        }
        
        //获取用户段位
        $arr = db('user')->where(['admin_id'=>$this->accessData['aid'],'id'=>$this->accessData['uid']])->field('paragraph')->find();
        $arr['answer'] = $res['answer'];
        
        //如果题目答完，跳到第一题
        //获取题目总数
        $number_info = db('riddle_title')->count('id');
        
        if ($post['id'] == $number_info){
            db('subject_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->update(['checkpoint'=>0]);
        }
        //关卡+1
        db('subject_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('checkpoint');
        //分数+1
        db('subject_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('fraction');
		
		//更新段位
		//获取当前闯关分数
		$str = db('subject_statistics')
				->where([
						'admin_id'=>$this->accessData['aid'],
						'user_id'=>$this->accessData['uid'],
						])
				->field('fraction')->find();
		
		//获取设置段位分数
		$wstr = db('riddle_paragraph')->where(['admin_id'=>$this->accessData['aid']])
				->field('id,name,number')->order('id asc')->select();

		$fraction=1;
		
		if($str['fraction'] >= $wstr[1]['number']  AND $str['fraction'] < $wstr[2]['number']){
			$fraction = 2;
		}elseif($str['fraction'] >= $wstr[2]['number']  AND $str['fraction'] < $wstr[3]['number']){
			$fraction = 3;
		}elseif($str['fraction'] >= $wstr[3]['number']  AND $str['fraction'] < $wstr[4]['number']){
			$fraction = 4;
		}elseif($str['fraction'] >= $wstr[4]['number']  AND $str['fraction'] < $wstr[5]['number']){
			$fraction = 5;
		}elseif($str['fraction'] >= $wstr[5]['number']  AND $str['fraction'] < $wstr[6]['number']){
			$fraction = 6;
		}elseif ($str['fraction'] >= $wstr[6]['number']){
		    $fraction = 7;
		}

		db('user')->where([
							'admin_id'=>$this->accessData['aid'],
							'id'=>$this->accessData['uid']
							])
					->update(['paragraph'=>$fraction]);
        suc('回答正确',$arr);
   
    }
    
    
    ##分享求助
    public function Seek_help()
    {
        $appid = db('customer')->where(['id'=>$this->accessData['aid']])->field('appid')->find();
        $apd = $appid['appid'];
        $post = request()->only(['id','type','encryptedData','vi'],'post');
        if (!empty($post['encryptedData'])){
            $encryptedData = $post['encryptedData'];
        }
        if (!empty($post['vi'])){
            $vi = $post['vi'];
        }

        //获得分享标语
		$list_info = db('configure')
		            ->where(['admin_id'=>$this->accessData['aid']])
					->field('shre_number_title,pe_number_ttile')
					->find();
		
        if (empty($post['id'])){
            //2018/05/18
            $dat['admin_id'] = $this->accessData['aid'];
            $dat['user_id'] = $this->accessData['uid'];
            $dat['share_number'] = 1;
            $dat['day'] = date("Y-m-d");
            db('all_statistics')->insert($dat);
            suc('分享成功');
        }
        
        if (empty($post['type'])){
            err('分享类型不能为空');
        }
        
        if ($post['type'] == 1){
            $title = "分享个人成功";
        }else{
            $title = "分享群成功";
        }
         
        $day = date("Y-m-d");
        $list = db('user')->field('session_key')->where(['admin_id'=>$this->accessData['aid'],'id'=>$this->accessData['uid']])->find();
        //分享到群
        if ($post['type'] == 2){
            
            $memberdecrypt = $this->getDecrypt($list['session_key'],$encryptedData,$vi,$apd);
            
            if($memberdecrypt['openGId']){
                $group_id = $memberdecrypt['openGId'];
            }
            //获取当前群分享次数
            $res_info = db('extension_statistics')
                        ->where([
                                'admin_id'=>$this->accessData['aid'],
                                'user_id'=>$this->accessData['uid'],
                                'create_time'=>$day,
                                'group_id'=>$group_id,
                                'type'=>1,
                                'sort'=>2])
                        ->count('id');
            //获取设置群分享次数
            $game_info = db('configure')->where(['admin_id'=>$this->accessData['aid']])->field('share_number')->find();
            
            if ($res_info >= $game_info['share_number']){
                
                //增加分享次数
                db('subject_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                //2018/05/18
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['share_number'] = 1;
                $dat['day'] = date("Y-m-d");
                db('all_statistics')->insert($dat);
                
                err($list_info['shre_number_title']);
            }
            
            //增加分享记录
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['extension_id'] = $post['id'];
            $data['type'] = 1;
            $data['sort'] = 2;
            $data['group_id'] = $group_id;
            $data['create_time'] = date("Y-m-d");
            
            $res = db('extension_statistics')->insert($data);
            
            if($res){
                $info = db('riddle_title')->where(['id'=>$post['id']])->find();
                $arr = db('user')->where(['admin_id'=>$this->accessData['aid'],'id'=>$this->accessData['uid']])->field('paragraph')->find();
                $arr['answer'] = $info['answer'];
                
                //增加分享次数
                db('subject_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                //2018/05/18
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['share_number'] = 1;
                $dat['day'] = date("Y-m-d");
                db('all_statistics')->insert($dat);
                
                suc($title,$arr);
            }
        
        //分享到个人    
        }elseif($post['type'] == 1){
            
            //获取分享次数
            $res_info = db('extension_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid'],'create_time'=>$day,'type'=>1,'sort'=>1])->count('id');
            //获取分享次数
            $game_info = db('configure')->where(['admin_id'=>$this->accessData['aid']])->field('pe_number')->find();

            if ($res_info >= $game_info['pe_number']){
                
                //增加分享次数
                db('subject_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                //2018/05/18
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['share_number'] = 1;
                $dat['day'] = date("Y-m-d");
                db('all_statistics')->insert($dat);
                
                err($list_info['pe_number_ttile']);
            }
            
            //增加分享记录
            $data['admin_id'] = $this->accessData['aid'];
            $data['user_id'] = $this->accessData['uid'];
            $data['extension_id'] = $post['id'];
            $data['type'] = 1;
            $data['sort'] = 1;
            $data['create_time'] = date("Y-m-d");
            
            $res = db('extension_statistics')->insert($data);
            
            if($res){
                $info = db('riddle_title')->where(['id'=>$post['id']])->find();
                $arr = db('user')->where(['admin_id'=>$this->accessData['aid'],'id'=>$this->accessData['uid']])->field('paragraph')->find();
                $arr['answer'] = $info['answer'];
                
                //增加分享次数
                db('subject_statistics')->where(['admin_id'=>$this->accessData['aid'],'user_id'=>$this->accessData['uid']])->setInc('share_number');
                
                //2018/05/18
                $dat['admin_id'] = $this->accessData['aid'];
                $dat['user_id'] = $this->accessData['uid'];
                $dat['share_number'] = 1;
                $dat['day'] = date("Y-m-d");
                db('all_statistics')->insert($dat);
                
                suc($title,$arr);
            }
        }
        
    }
    
    ##排行榜
    public function ranking()
    {
        $where['b.avatarurl'] = ['neq',""];
		$where['b.nickname'] = ['neq',""];
		$where['a.admin_id'] = $this->accessData['aid'];
		$where['a.user_id'] = ['neq',$this->accessData['uid']];
        $res['all'] = db('subject_statistics')->alias('a')->join('user b','a.user_id = b.id','left')
                ->field('b.avatarurl,b.nickname,b.paragraph,a.checkpoint')
                ->where($where)
                ->order('a.checkpoint desc')
                ->limit(50)
                ->select();
        foreach($res['all'] as $key => $val){
            if(mb_strlen($val['nickname'], 'utf8') > 5){
                $res['all'][$key]['nickname'] = mb_substr($val['nickname'], 0, 5, 'utf8').'...';
                
            }
            
        }
        
        //用户个人排名
        $arr = db('subject_statistics')->where(['admin_id'=>$this->accessData['aid']])->order("checkpoint desc")->select();
        
        $numbers = "";
        foreach ($arr as $k => $v){
            if ($v['user_id'] == $this->accessData['uid']){
                $numbers = $k+1;
            }
        }
        //用户个人排行
        $where = [];
        $where['a.admin_id'] = $this->accessData['aid'];
        $where['a.user_id'] = $this->accessData['uid'];
        $res['my'] = db('subject_statistics')->alias('a')->join('user b','a.user_id = b.id','left')
                    ->field('b.avatarurl,b.nickname,b.paragraph,a.checkpoint')
                    ->where($where)
                    ->order('a.checkpoint desc')
                    ->find();
        $res['my']['numbers'] = $numbers;
        if (!$res){
            err('暂无数据');
        }else{
            suc('',$res);
        }
    }
    
    ##更多好玩
    public function extension()
    {
        $res = db('extension')
                ->where(['sort'=>1,'type'=>0,'admin_id'=>$this->accessData['aid']])
                ->field('id,title,img,content,url')
                ->order('sort_t desc')
                ->select();
        
        foreach ($res as $k => $v){
            $res[$k]['img'] = $this->game_img($v['img']);
        }
        
        if($res){
            suc('',$res);
        }else{
            err('暂未开放');
        }
    }
    
    ##推广游戏跳转
    public function share()
    {
        $post = request()->only(['id'],'post');
        
        if (empty($post['id'])){
            err('游戏ID不能为空');
        }
        
        $data['admin_id'] = $this->accessData['aid'];
        $data['user_id'] = $this->accessData['uid'];
        $data['extension_id'] = $post['id'];
        $data['create_time'] = date("Y-m-d");
        $data['type'] = 2;
        
        $res = db('extension_statistics')->insert($data);
        
        if (!$res){
            err('失败');
        }else{
            suc('成功');
        }
    }
  
    
    ##分享获得图片
    public function image()
    {
        $res = db('configure')->where(['admin_id'=>$this->accessData['aid']])->field('share_img,share_title')->find();
    

        $res['share_img'] = $this->game_img($res['share_img']);

    
        if (!$res){
            err('暂无数据');
        }else{
            suc('',$res);
        }
    }
    
    
    
    
    
    
}